<template>
  <!-- <div class="skill-container" style="padding: 0"> -->
  <el-card>
    <el-form>
      <el-row type="flex" justify="space-between" style="margin-bottom: -10px">
        <el-form-item
          label="文章名称"
          label-width="70px"
          style="font-weight: 700"
        >
          <el-input
            v-model="queryData.keyword"
            size="small"
            placeholder="请根据文章标题搜索"
            style="width: 200px; margin-right: 120px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="resetSearch">清除</el-button>
          <el-button
            type="primary"
            size="small"
            @click="initData"
            style="margin-right: 400px"
            >搜索</el-button
          >
        </el-form-item>
        <div style="flex: 1"></div>
        <!-- 新增技巧按钮 -->
        <el-form-item>
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="showDialog = true"
            >新增技巧</el-button
          >
        </el-form-item>
      </el-row>
      <el-form-item label="状态" label-width="70px" style="font-weight: 700">
        <el-select v-model="queryData.state" size="small" placeholder="请选择">
          <el-option label="已启用" value="1"> </el-option>
          <el-option label="已禁用" value="0"> </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <page-tools :text="`数据一共${total}条`" />
    <!-- 表格区域 -->
    <el-table border :data="list" style="margin-bottom: 10px">
      <el-table-column
        label="序号"
        type="index"
        prop="id"
        width="60"
      ></el-table-column>
      <el-table-column
        label="文章标题"
        prop="title"
        width="300"
      ></el-table-column>
      <el-table-column
        label="阅读数"
        prop="visits"
        width="120"
      ></el-table-column>
      <el-table-column label="录入人" prop="username"></el-table-column>
      <el-table-column label="录入时间" prop="createTime">
        <template slot-scope="{ row }">
          {{ row.createTime | changeTime }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state">
        <template slot-scope="scope">
          <div>{{ scope.row.state * 1 === 1 ? "已启用" : "已禁用" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="preview(scope.row)"
            >预览</el-button
          >
          <el-button type="text" size="small" @click="editState(scope.row)">{{
            scope.row.state * 1 === 0 ? "启用" : "禁用"
          }}</el-button>
          <el-button
            :disabled="scope.row.state * 1 === 1"
            type="text"
            size="small"
            @click="editHandler(scope.row)"
            >修改</el-button
          >
          <el-button
            :disabled="scope.row.state * 1 === 1"
            type="text"
            size="small"
            @click="delHandler(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      style="float: right; margin-bottom: 10px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.page"
      :page-sizes="[10, 8, 6]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增/修改弹框区域 -->
    <el-dialog
      :title="this.formData.id ? '编辑文章' : '新增文章'"
      :visible.sync="showDialog"
      @close="close"
    >
      <el-form
        v-if="showDialog"
        style="border-radius: 5px"
        label-width="120px"
        :model="formData"
        :rules="formRules"
        ref="form"
      >
        <el-form-item prop="title" label="文章标题" style="font-weight: 700">
          <el-input
            size="small"
            v-model="formData.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item
          prop="articleBody"
          label="文章内容"
          style="font-weight: 700"
        >
          <quillEditor
            ref="myQuillEditor"
            v-model="formData.articleBody"
            :options="editorOption"
          ></quillEditor>
        </el-form-item>
        <el-form-item prop="videoURL" label="视频地址" style="font-weight: 700">
          <el-input
            size="small"
            v-model="formData.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <!-- 取消提交按钮 -->
        <el-button @click="showDialog = false" size="small">取消</el-button>
        <!-- 提交按钮 -->
        <el-button @click="btnOk" type="primary" size="small">确定</el-button>
      </el-row>
    </el-dialog>
    <!-- 预览弹框 -->
    <el-dialog title="预览文章" :visible.sync="showPreview" @close="close">
      <div class="preview-box">
        <el-row class="preview-title" style="margin: -20px 0 -10px">
          <h2>{{ prelist.title }}</h2>
        </el-row>
        <el-row
          class="preview-nav"
          type="flex"
          justify="start"
          style="margin-bottom: 5px"
        >
          <div style="margin-right: 10px">
            {{ prelist.createTime | changeTime }}
          </div>
          <div style="margin-right: 10px">{{ prelist.username }}</div>
          <div style="margin-right: 10px">
            <i class="el-icon-view"></i>
          </div>
          <div>{{ prelist.visits }}</div>
        </el-row>
        <div
          style="background: rgba(245, 245, 245, 1); padding: 10px 5px"
          class="preview-content"
          v-html="prelist.articleBody"
        ></div>
      </div>
    </el-dialog>
  </el-card>
  <!-- </div> -->
</template>

<script>
import {
  getArticleListApi,
  getArticleInfoApi,
  addArticleApi,
  editArticleApi,
  delArticleInfoApi,
  editArticleStateApi,
} from "@/api/Skill";

import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
export default {
  name: "Skill",
  components: {
    quillEditor,
  },
  data() {
    return {
      prelist: [],
      showDialog: false,
      showPreview: false,
      list: [],
      total: 0,
      queryData: {
        page: 1,
        pagesize: 10,
        keyword: "",
      },
      formData: {
        title: "",
        articleBody: "",
        videoURL: "",
      },
      formRules: {
        title: [{ required: true, message: "请输入", trigger: "blur" }],
        articleBody: [{ required: true, message: "请输入", trigger: "blur" }],
      },
      editorOption: {
        // 占位配置
        placeholder: "Insert text here...",
        modules: {
          // 配置工具栏
          toolbar: [
            ["bold", "italic", "underline", "strike"],
            ["blockquote", "code-block"],
            [{ header: 1 }, { header: 2 }],
            [{ list: "ordered" }, { list: "bullet" }],
            [{ indent: "-1" }, { indent: "+1" }],
            ["image"],
          ],
        },
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    // 文章列表数据渲染
    async initData() {
      let res = await getArticleListApi(this.queryData);
      console.log(res);
      this.total = res.counts;
      this.list = res.items;
    },
    // 页码变化事件
    handleCurrentChange(val) {
      this.queryData.page = val;
      this.initData();
    },
    // 每页数量变化事件
    handleSizeChange(val) {
      // val 最新每页数量
      this.queryData.pagesize = val;
      this.initData();
    },
    // 清空搜索框
    resetSearch() {
      this.queryData = {};
      this.state = "";
      this.initData();
    },
    // 修改当前行数据
    async editHandler(val) {
      // val是当前点击行的数据
      // 发送查询当前点击行数据的请求
      let res = await getArticleInfoApi(val.id);
      console.log(res);
      // 赋值给表单
      this.formData = res;
      // 弹框打开
      this.showDialog = true;
    },
    // 事件提交
    async btnOk() {
      // 表单校验
      try {
        await this.$refs.form.validate();
      } catch (error) {
        return console.log(error);
      }
      // 发送请求，判断不同的逻辑
      if (this.formData.id) {
        // 有当前点击行id，则为修改功能
        await editArticleApi(this.formData);
      } else {
        // 无当前点击行id，即为新增功能
        await addArticleApi(this.formData);
      }
      // 提示成功
      this.$message.success("操作成功");
      // 关闭弹框
      this.showDialog = false;
      // 更新列表
      this.initData();
    },
    // 弹框关闭后重置表单
    close() {
      this.formData = {};
    },
    // 删除事件
    async delHandler(id) {
      // 弹出确认框
      try {
        await this.$confirm("是否确认删除？", "提示", {
          type: "warning",
        });
      } catch (error) {
        return this.$message("你取消了删除操作");
      }
      // 发送删除请求，传递删除id
      await delArticleInfoApi(id);
      // 提示成功
      this.$message.success("删除成功");
      // 更新列表
      this.initData();
    },
    // 改变启用/禁用状态的事件
    async editState(val) {
      const state = val.state * 1 === 1 ? 0 : 1;
      await editArticleStateApi({
        id: val.id,
        state,
      });
      this.$message.success("操作成功");
      this.initData();
    },
    // 预览功能
    preview(val) {
      this.prelist = val;
      this.showPreview = true;
    },
  },
};
</script>

<style lang="less">
.el-dialog__header {
  background-color: #409eff !important;
  .el-dialog__title {
    color: #fff;
  }
  .el-dialog__close {
    color: #fff !important;
  }
}
.preview-nav {
  .el-col {
    .el-icon-view {
      color: #000;
    }
  }
}
</style>